package com.yzjs.wx.controller;



import com.yzjs.base.PayResponse;
import com.yzjs.config.WxPayConfig;
import com.yzjs.enumeration.BestPayTypeEnum;
import com.yzjs.wx.entity.PayInfo;
import com.yzjs.wx.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("pay")
public class PayController {

    @Autowired
    private PayService payService;

    @Autowired
    private WxPayConfig wxPayConfig;

    @GetMapping("create")
    public Map create(
            @RequestParam("openId") String openId ,
            @RequestParam("orderNo") String orderNo ,
                               @RequestParam("amount")BigDecimal orderAmout ){
        PayResponse payResponse = payService.create(openId,orderNo, orderAmout , BestPayTypeEnum.WXPAY_NATIVE);
        Map map = new HashMap<>();
        map.put("codeUrl" , payResponse.getCodeUrl());
        map.put("orderId",orderNo);
        map.put("returnUrl",wxPayConfig.getReturnUrl());
        return map;
    }

    @PostMapping("notify")
    public String asyncNotify(@RequestBody String notifyData){
        return payService.asyncNotify(notifyData);
    }

    @GetMapping("queryByOrderId")
    public PayInfo queryByOrderId(@RequestParam("orderNo") Long orderNo){
        return payService.queryByOrderId(orderNo);
    }
}
